#include <Lucnx/types.h>
#include <Lucnx/printk.h>
#include <Lucnx/fifo.h>
#include <Lucnx/task.h>
#include <Lucnx/syscall.h>
#include <Lucnx/device.h>
#include <Lucnx/keyboard.h>
#include <Lucnx/debug.h>
#include <Lucnx/system.h>
#include <Lucnx/acpi.h>

void *syscall_table[128];

size_t sys_write(fd_t fd, char *buf, size_t nr)
{
    if (fd == stdout)
    {
        dev_t dev = device_search("tty");
        return device_write(dev, NULL, buf, nr);
    }
    printk("not file system\n");
}

void sys_clear()
{
    console_clear();
}

size_t sys_read(fd_t fd, char *buf, size_t nr)
{
    if (fd == stdin)
    {
        dev_t dev = device_search("tty");
        return device_read(dev, NULL, buf, nr);
    }
    printk("no file system\n");
}

void sys_reboot(int fno)
{
    acpi_shutdown();
}

void sys_test()
{
    printk("syscall test\n");
}

void syscall_init()
{
    syscall_table[SYS_NR_TEST] = sys_test;
    syscall_table[SYS_NR_WRITE] = sys_write;
    syscall_table[SYS_NR_CLEAR] = sys_clear;
    syscall_table[SYS_NR_READ] = sys_read;
    syscall_table[SYS_NR_GETPID] = sys_getpid;
    syscall_table[SYS_NR_SLEEP] = task_sleep;
    syscall_table[SYS_NR_EXIT] = sys_exit;
    syscall_table[SYS_NR_SYSINFO] = sys_sysinfo;
    syscall_table[SYS_NR_BRK] = sys_brk;
    syscall_table[SYS_NR_FORK] = sys_fork;
    syscall_table[SYS_NR_GETPPID] = sys_getppid;

    INFOK("INFO", "System calls init done");
}